package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.exception.NegativeNumberException;
import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 票卡更新数据明细表实体类 作者: 日期：2018-08-12 17:14:29 描述：
 */
public class TradeUpdate extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 无入站进站站点
	 */
	private String enterAddress;

	/*
	 * 前次出站时间
	 */
	private String exitTime;

	/*
	 * 前次出站终端编号
	 */
	private String exitDeviceId;

	/*
	 * 前次交易类型
	 */
	private String exTradeCode;

	/*
	 * 前次交易时间
	 */
	private String exTradeTime;

	/*
	 * 前次交易金额
	 */
	private Long exTradeMoney;

	/*
	 * 前次交易操作员号
	 */
	private String exOper;

	/*
	 * 卡当前核准状态
	 */
	private String approveStatus;

	/*
	 * 卡当前核准时间
	 */
	private String approveTime;

	/*
	 * 卡当前核准站点
	 */
	private String approveStation;

	/*
	 * 审核员ID
	 */
	private String operId;

	/*
	 * 卡当前进出站标志
	 */
	private String enterFlag;

	/*
	 * 前次进出站站点
	 */
	private String exTradeAddress;

	/*
	 * 前次交易全局设备ID
	 */
	private String exDeviceId;

	/**
	 * 默认构造方法
	 **/
	public TradeUpdate() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getEnterAddress() {
		return enterAddress;
	}

	public void setEnterAddress(String enterAddress) {
		this.enterAddress = enterAddress;
	}

	public String getExitTime() {
		return exitTime;
	}

	public void setExitTime(String exitTime) throws ParseException {
		checkTimeStyle(exitTime, "ExitTime");
		this.exitTime = exitTime;
	}

	public String getExitDeviceId() {
		return exitDeviceId;
	}

	public void setExitDeviceId(String exitDeviceId) {
		this.exitDeviceId = exitDeviceId;
	}

	public String getExTradeCode() {
		return exTradeCode;
	}

	public void setExTradeCode(String exTradeCode) {
		this.exTradeCode = exTradeCode;
	}

	public String getExTradeTime() {
		return exTradeTime;
	}

	public void setExTradeTime(String exTradeTime) throws ParseException {
		checkTimeStyle(exTradeTime, "ExTradeTime");
		this.exTradeTime = exTradeTime;
	}

	public Long getExTradeMoney() {
		return exTradeMoney;
	}

	public void setExTradeMoney(Long exTradeMoney) throws NegativeNumberException {
		checkMoneyStyle(exTradeMoney, "exTradeMoney");
		this.exTradeMoney = exTradeMoney;
	}

	public String getExOper() {
		return exOper;
	}

	public void setExOper(String exOper) {
		this.exOper = exOper;
	}

	public String getApproveStatus() {
		return approveStatus;
	}

	public void setApproveStatus(String approveStatus) {
		this.approveStatus = approveStatus;
	}

	public String getApproveTime() {
		return approveTime;
	}

	public void setApproveTime(String approveTime) throws ParseException {
		checkTimeStyle(approveTime, "ApproveTime");
		this.approveTime = approveTime;
	}

	public String getApproveStation() {
		return approveStation;
	}

	public void setApproveStation(String approveStation) {
		this.approveStation = approveStation;
	}

	public String getOperId() {
		return operId;
	}

	public void setOperId(String operId) {
		this.operId = operId;
	}

	public String getEnterFlag() {
		return enterFlag;
	}

	public void setEnterFlag(String enterFlag) {
		this.enterFlag = enterFlag;
	}

	public String getExTradeAddress() {
		return exTradeAddress;
	}

	public void setExTradeAddress(String exTradeAddress) {
		this.exTradeAddress = exTradeAddress;
	}

	public String getExDeviceId() {
		return exDeviceId;
	}

	public void setExDeviceId(String exDeviceId) {
		this.exDeviceId = exDeviceId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeUpdate init(String message) throws Exception {



		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {

			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 6, 14, 10, 8, 8, 8, 8, 5, 10, 10, 3, 14, 9, 10, 9, 2, 14, 8, 6, 1,
					14, 10, 20, 10, 2, 2, 16, 1, 1, 2, 2, 2, 4, 4, 1, 4, 5, -1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);

			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
			this.setCost(Long.parseLong(messageParser.getFiled(12)));
			this.setBalance(Long.parseLong(messageParser.getFiled(13))); // 卡余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(14)));

			this.setEnterAddress(messageParser.getFiled(15));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(16)));
			this.setEnterFlag(messageParser.getFiled(17)); // 当前进出站标志
			this.setExitTime(messageParser.getFiled(18));
			this.setExitDeviceId(messageParser.getFiled(19));

			this.setExTradeAddress(messageParser.getFiled(20));
			this.setExDeviceId(messageParser.getFiled(21));
			this.setExTradeCode(messageParser.getFiled(22));
			this.setExTradeTime(messageParser.getFiled(23));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(24)));

			this.setExOper(messageParser.getFiled(25));
			this.setApproveStatus(messageParser.getFiled(26));
			this.setApproveTime(messageParser.getFiled(27));
			this.setApproveStation(messageParser.getFiled(28));

			this.setSamcardId(messageParser.getFiled(29));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(30)));
			this.setCardCode(messageParser.getFiled(31));
			this.setTradeSub(messageParser.getFiled(32));
			this.setPhysicalId(messageParser.getFiled(33));

			this.setTestFlag(messageParser.getFiled(34));
			this.setGreyFlag(messageParser.getFiled(35));
			this.setMedia(messageParser.getFiled(36));
			this.setCardVersion(messageParser.getFiled(37));
			this.setSubCardCode(messageParser.getFiled(38));

			this.setCityCode(messageParser.getFiled(39));
			this.setBusinessCode(messageParser.getFiled(40));

			this.setIndustrySplit(messageParser.getFiled(41));
			this.setIssuer(messageParser.getFiled(42));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(43)));
			super.setIndustryData(messageParser.getFiled(44));
			super.setCrc16(messageParser.getFiled(45));

		} else if(message.startsWith("SZMETROV3")) {

			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
			 付费方式
			 支付方式
			 发卡方机构码
			 发卡方交易流水号
			 运营模式
			 三、票卡发行方标识	从长度4变为长度2
			 */
			int[] splitNumber = {
					9, 8, 20, 2, 10,   9, 6, 6, 14, 10,   8, 8, 8, 8, 5,   10, 10, 3, 14, 9,   10, 9, 2, 14, 8,
					6, 1, 14, 10, 20,  10, 2, 2, 16, 1,   1, 2, 2, 2, 4,   4,
					1, 2, 8, 10, 4,    //V3增加的字段
					1, 2, 5,
					-1, 5};
			messageParser = new TradeMessageParser(message, splitNumber);

			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
			this.setCost(Long.parseLong(messageParser.getFiled(12)));
			this.setBalance(Long.parseLong(messageParser.getFiled(13))); // 卡余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(14)));

			this.setEnterAddress(messageParser.getFiled(15));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(16)));
			this.setEnterFlag(messageParser.getFiled(17)); // 当前进出站标志
			this.setExitTime(messageParser.getFiled(18));
			this.setExitDeviceId(messageParser.getFiled(19));

			this.setExTradeAddress(messageParser.getFiled(20));
			this.setExDeviceId(messageParser.getFiled(21));
			this.setExTradeCode(messageParser.getFiled(22));
			this.setExTradeTime(messageParser.getFiled(23));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(24)));

			this.setExOper(messageParser.getFiled(25));
			this.setApproveStatus(messageParser.getFiled(26));
			this.setApproveTime(messageParser.getFiled(27));
			this.setApproveStation(messageParser.getFiled(28));

			this.setSamcardId(messageParser.getFiled(29));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(30)));
			this.setCardCode(messageParser.getFiled(31));
			this.setTradeSub(messageParser.getFiled(32));
			this.setPhysicalId(messageParser.getFiled(33));

			this.setTestFlag(messageParser.getFiled(34));
			this.setGreyFlag(messageParser.getFiled(35));
			this.setMedia(messageParser.getFiled(36));
			this.setCardVersion(messageParser.getFiled(37));
			this.setSubCardCode(messageParser.getFiled(38));

			this.setCityCode(messageParser.getFiled(39));
			this.setBusinessCode(messageParser.getFiled(40));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(41));
			this.setPayType(messageParser.getFiled(42));
			this.setIssuerCode(messageParser.getFiled(43));
			this.setIssuerSeq(messageParser.getFiled(44));
			this.setOperMode(messageParser.getFiled(45));

			this.setIndustrySplit(messageParser.getFiled(46));
			this.setIssuer(messageParser.getFiled(47));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(48)));
			super.setIndustryData(messageParser.getFiled(49));
			super.setCrc16(messageParser.getFiled(50));

		}

		return this;
	}
}